package org.csu.chatserver.mapper;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;
import org.csu.chatserver.entity.dto.UnreadCountDTO;
import org.csu.chatserver.entity.pojo.SingleMessage;
import org.csu.chatserver.entity.vo.HistoryMsgVO;

import java.util.List;

@Mapper
public interface SingleMessageMapper extends BaseMapper<SingleMessage> {
    List<HistoryMsgVO> getHistoriesMsg(int senderId, int targetId);

    @Update("UPDATE single_message SET is_read = 1 WHERE sender_id = #{senderId} AND target_id = #{targetId} AND is_read = 0")
    void markMessagesAsRead(@Param("senderId") int senderId, @Param("targetId") int targetId);

    // 获取未读数：统计 target_id = 当前用户，按 sender_id 分组
    @Select("SELECT sender_id AS senderId, COUNT(*) AS unreadCount " +
            "FROM single_message " +
            "WHERE target_id = #{targetId} AND is_read = 0 " +
            "GROUP BY sender_id")
    List<UnreadCountDTO> getUnreadCounts(@Param("targetId") int targetId);

    // 删除指定用户间的聊天记录
    @Delete("DELETE FROM single_message WHERE (sender_id = #{senderId} AND target_id = #{targetId}) OR (sender_id = #{targetId} AND target_id = #{senderId})")
    int clearHistory(@Param("senderId") Long senderId, @Param("targetId") Long targetId);
}
